#include "privatechatmanager.h"
#include <QApplication>

PrivateChatManager::PrivateChatManager(QObject *parent) : QObject(parent)
{
}

PrivateChatManager::~PrivateChatManager()
{
    // Delete private chat windows
    for( int i = 0; i < privateChatWindows.size(); ++i )
    {
        delete privateChatWindows[i];
    }
}

void PrivateChatManager::newPrivateChat(const QString &friend_loginName)
{
    if( getPrivateChat(friend_loginName) )
    {
        // Restore the window and bring it to the front of the application
        QApplication::setActiveWindow(getPrivateChat(friend_loginName));
        getPrivateChat(friend_loginName)->showNormal();
        return;
    }

    privateChatWindows.resize(privateChatWindows.size() + 1);
    privateChatWindows.at(privateChatWindows.size() - 1) = new PrivateChatWindow(friend_loginName);
    connect( privateChatWindows.at(privateChatWindows.size() - 1), SIGNAL(sendMessageToFriend(QString, QString)),
             this, SLOT(on_sendMessageToFriend(QString, QString)) );
    connect( privateChatWindows.at(privateChatWindows.size() - 1), SIGNAL(privateChatWindowClose(QString)), this, SLOT(on_privateChatWindowClose(QString)) );
    privateChatWindows.at(privateChatWindows.size() - 1)->show();
}

PrivateChatWindow* PrivateChatManager::getPrivateChat(const QString &friend_loginName)
{
    for( int i = 0; i < privateChatWindows.size(); ++i )
    {
        if( privateChatWindows[i]->getParticipantLoginName() == friend_loginName )
            return privateChatWindows[i];
    }

    return NULL;
}

int PrivateChatManager::getPrivateChatIndex(const QString &friend_loginName)
{
    for( int i = 0; i < privateChatWindows.size(); ++i )
    {
        if( privateChatWindows[i]->getParticipantLoginName() == friend_loginName )
            return i;
    }

    return -1;
}

void PrivateChatManager::deletePrivateChat(const QString &friend_loginName)
{
    if( getPrivateChatIndex(friend_loginName) < 0 )
        return;

    delete getPrivateChat(friend_loginName);

    for( int i = getPrivateChatIndex(friend_loginName); i < privateChatWindows.size() - 1; ++i )
    {
        privateChatWindows[i] = privateChatWindows[i + 1];
    }

    privateChatWindows.resize(privateChatWindows.size() - 1);
}

void PrivateChatManager::on_sendMessageToFriend(const QString &friend_loginName, const QString &message)
{
    emit sendMessageToFriend(friend_loginName, message);
}

void PrivateChatManager::on_newPrivateMessage(const QString &sender, const QString &message, const QString &to)
{
    PrivateChatWindow *chatWindow = getPrivateChat(sender);

    if( chatWindow )
    {
        chatWindow->addChatMessage(sender, message);
    }
    else
    {
        newPrivateChat(sender);
        on_newPrivateMessage(sender, message, to);
    }
}

void PrivateChatManager::on_privateChatWindowClose(const QString &friend_loginName)
{
    deletePrivateChat(friend_loginName);
}
